home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
THINKC
/
5
/
TIME_ZON
/
CNAMELIS.C
< prev
next >
Wrap
Text File
|
1991-11-08
|
9KB
|
424 lines
/* CNameList.c
* This object is a subclass of CList. It has the added ability to deal
* with its listed objects by their instance variable 'name'.
*/
#include "CNameList.h"
#include "CListBox.h"
#include <GenRoutines.h>
void
CNameList::INameList()
{
inherited::IList();
/*
* There is currently no new function, and no owner
*/
newFunc = NULL;
owner = NULL;
}
void
CNameList::SetNewFunc(theFunc, listOwner)
CNObject * (* theFunc)();
CObject * listOwner;
{
/*
* The function passed in will be called when a new object needs
* to be created, and the owner object will be passed to it.
*/
newFunc = theFunc;
owner = listOwner;
}
int
CNameList::Insert(theObject)
CNObject *theObject;
{
CNObject *otherObj;
long place;
/*
* Find the first name which is greater than the input name:
*/
otherObj = (CNObject *) FirstSuccess1(&NG,(long)theObject->name);
/*
* If there isn't one, add this at the end. Otherwise, insert
* it at this location, to keep things in order by name.
*/
if (otherObj == NULL)
Append(theObject);
else {
place = FindIndex(otherObj);
InsertAt(theObject,place);
}
/*
* Return the index of the new location
*/
return(FindIndex(theObject));
}
static Boolean
NG(theObject,theName)
CNObject *theObject;
long theName;
{
/*
* Is the name of this object greater than the input name?
*/
return(theObject->NameGreater((char *) theName));
}
CNObject *
CNameList::FindName(theName)
char *theName;
{
CNObject *theObject;
Boolean NE();
/*
* Find the first successful name match
*/
return((CNObject *) FirstSuccess1(&NE, (long) theName));
}
static Boolean
NE(theObject,theName)
CNObject *theObject;
long theName;
{
/*
* Is this object name equal to the input name?
*/
return(theObject->NameEquals((char *) theName));
}
CNObject *
CNameList::SelectName()
{
#define CGEDLG 302
#define SELECT 1
#define DONE 2
DialogPtr thedlg;
GrafPtr save;
int which, res, ect, ict, where;
short thetype;
Handle theitem;
Rect thebox, db;
CNObject *theObject;
CListBox *theLBox;
CNameList *theLBList;
char result[256];
/*
* This routine presents a dialog from which the name of one
* of the items in the list can be selected.
*/
/*
* First, get a dialog
*/
ParamText("\pChoose","","","");
thedlg = GetNewDialog(CGEDLG,NULL,(WindowPtr) -1);
install_useritem(thedlg,(short) 3,(ProcPtr) &outline_ok);
/*
* Make a list for the CListBoxes in this dialog. Actually there
* is only one, but the CListBox class is designed to be able
* to handle more than one.
*/
theLBList = new(CNameList);
theLBList->INameList();
/*
* Obtain the number of items in the list, and make up the
* CListBox with that many cells. Make it item 4 in the dialog
* since that's how the dialog is defined.
*/
ect = GetNumItems();
GetDItem(thedlg,4,&thetype,&theitem,&thebox);
theLBox = new(CListBox);
theLBox->IListBox((WindowPtr) thedlg,&thebox,ect, 4);
install_useritem(thedlg,(short) 4,(ProcPtr) &draw_list);
/*
* Insert the names of each named object into the CListBox,
* in order
*/
for(ict = 1;ict <= ect; ict++) {
theObject = (CNObject *) NthItem(ict);
theLBox->SetCell(ict,theObject->name);
}
/*
* Add this CListBox to the list of CListBoxes, even though
* it's the only one. Also, make sure that the list of
* CListBoxes is available to the dialog when needed.
*/
theLBList->Append(theLBox);
SetWRefCon((WindowPtr) thedlg,(long) theLBList);
/*
* I'm not sure why all this is necessary, but it seems to be...
*/
GetPort(&save);
SelectWindow(thedlg);
ShowWindow(thedlg);
SetPort(thedlg);
/*
* Here we go! Notice that there is a filter routine (check_list in
* GenRoutines.c) which handles the action for the listbox.
*/
theObject = NULL;
which = 0;
while ((which != SELECT) && (which != DONE)) {
ModalDialog(&check_list,&which);
/*
* Okay, we got one, unless the user selected nothing, in
* which case we'll send back NULL
*/
if (which == SELECT) {
theLBox->GetCell(result);
if (result[0] != '\0')
theObject = FindName(result);
}
}
/*
* Dont' need the listbox stuff anymore
*/
theLBox->Dispose();
theLBList->Dispose();
/*
* Don't need the dialog any more
*/
DisposDialog(thedlg);
SetPort(save);
return(theObject);
}
Boolean
CNameList::Edit()
{
#define CELDLG 301
#define EDIT 1
#define CANCEL 2
#define NEW 5
#define DELETE 6
DialogPtr thedlg;
GrafPtr save;
int which, res, ect, ict, where;
short thetype;
Handle theitem;
Rect thebox, db;
CNObject *theObj;
CListBox *theLBox;
CList *theLBList;
char result[256];
Boolean modified = FALSE;
/*
* This dialog allows the user to add, remove, and edit
* individual name items in the CNameList, even though the
* CNameList doesn't know anything about them except that they
* have names.
*/
/*
* First, get the dialog
*/
thedlg = GetNewDialog(CELDLG,NULL,(WindowPtr) -1);
install_useritem(thedlg,(short) 3,(ProcPtr) &outline_ok);
/*
* The CListBox class allows multiple listboxes in one dialog,
* so we'll need a new list for them, even though we'll only be
* using one.
*/
theLBList = new(CList);
theLBList->IList();
/*
* Get the number of items in our list, and put the newly made
* listbox into the dialog at position 4 (since that's where
* we said it would be in the dialog definition)
*/
ect = GetNumItems();
GetDItem(thedlg,4,&thetype,&theitem,&thebox);
theLBox = new(CListBox);
theLBox->IListBox((WindowPtr) thedlg,&thebox,ect,4);
install_useritem(thedlg,(short) 4,(ProcPtr) &draw_list);
/*
* Load up the cells with the names of the objects in the
* list in order by name.
*/
for(ict = 1;ict <= ect; ict++) {
theObj = (CNObject *) NthItem(ict);
theLBox->SetCell(ict,theObj->name);
}
/*
* Add this listbox to the list of listboxes, even though it
* will be the only one. Then save the list of listboxes in the
* dialog so it will be available when needed.
*/
theLBList->Append(theLBox);
SetWRefCon((WindowPtr) thedlg,(long) theLBList);
/*
* See my note for SelectName.
*/
GetPort(&save);
SelectWindow(thedlg);
ShowWindow(thedlg);
SetPort(thedlg);
/*
* Here we go! Note that there is a filter named check_list for
* the ModalDialog call, which can be found in GenRoutines.
*/
which = NEW;
while (which != CANCEL) {
/*
* We reset the param text just in case the editing of the
* individual objects might have re-done it
*/
ParamText("\pEdit Time Zones","","","");
ModalDialog(&check_list,&which);
switch (which) {
case NEW:
/*
* So okay, we make a new one by calling the
* function passed in by the real owner of the
* CNameList. Then we call the object's own edit
* routine to edit itself, then insert it according
* to the name.
*/
theObj = (CNObject *) (*newFunc)(owner);
theObj->Edit();
where = Insert(theObj);
theLBox->InsertCell(where,theObj->name);
modified = TRUE;
break;
case CANCEL:
break;
case EDIT:
case DELETE:
/*
* Try to find the selected cell, and if found,
* edit or delete, depending on the user's choice
*/
theLBox->GetCell(result);
if (result[0] != '\0') {
theObj = (CNObject *) FindName(result);
if (theObj != NULL) {
if (which == EDIT) {
/*
* Okay, call the thing to edit itself,
* and then replace it in the list in
* case its name was changed
*/
theObj->Edit();
where = FindIndex(theObj);
theLBox->RemoveCell(where);
Remove(theObj);
where = Insert(theObj);
theLBox->InsertCell(where,theObj->name);
modified = TRUE;
}
else { /* it must be a DELETE */
/* Okay, find it and remove it from
* the listbox, and from the CNameList
* as well. DisposeComplete is a CNObject
* call which gets rid of everything about
* the object. See CZone for the reason.
*/
where = FindIndex(theObj);
theLBox->RemoveCell(where);
Remove(theObj);
theObj->DisposeComplete();
modified = TRUE;
}
}
}
break;
}
}
/*
* Don't need this stuff any more
*/
theLBox->Dispose();
theLBList->Dispose();
/*
* Or this
*/
DisposDialog(thedlg);
SetPort(save);
return(modified);
}